﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections;
using Telerik.Windows.Controls;
using TestMatrix.Model;
using TestMatrix.BLController;

namespace TestMatrix.Phone
{
    public partial class GlobalTestCasePassRatePage : PhoneApplicationPage
    {
        public GlobalTestCasePassRatePage()
        {
            InitializeComponent();
            this.Loaded += OnLoad;

            ShowFinancialyearList();
            ShowProjectList();
        }

        private void ShowFinancialyearList()
        {
            string sql = "select distinct Financialyear from tblGlobalTestCasePassRate order by Financialyear";

            List<tblGlobalTestCasePassRate> listDatas = ChartController.QuerytblGolbalTestCasePassRate(sql);
            List<string> Sor = new List<string>();
            foreach (var item in listDatas)
            {
                Sor.Add(item.Financialyear.ToString());
            }
            this.listFY.ItemsSource = Sor;
        }

        private void ShowProjectList()
        {
            string sql = "select distinct ProjectName from tblGlobalTestCasePassRate order by ProjectName";

            List<tblGlobalTestCasePassRate> listDatas = ChartController.QuerytblGolbalTestCasePassRate(sql);
            List<string> Sor = new List<string>();
            foreach (var item in listDatas)
            {
                Sor.Add(item.ProjectName.ToString());
            }
            this.listProjectName.ItemsSource = Sor;
        }

        string strConditionFY = "";
        private void listFY_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IList data = (sender as ListPicker).SelectedItems;
            strConditionFY = FilterSettingController.AppendMultipleConditions(data, " and Financialyear in (");
            Refresh();
        }


        string strConditionProjectName = "";
        private void listProjectName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IList data = (sender as ListPicker).SelectedItems;
            strConditionProjectName = FilterSettingController.AppendMultipleConditions(data, " and ProjectName in (");
            Refresh();
        }

        private void Refresh()
        {
            System.Threading.ThreadPool.QueueUserWorkItem(this.LoadItemsSource);
            this.isDisplayed = true;
            this.OnDisplayed();
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            if (this.ListBox != null)
            {
                this.ListBox.IsAsyncBalanceEnabled = this.EnableAsyncBalance;
            }

            this.isDisplayed = true;
            this.OnDisplayed();
        }

        protected virtual IEnumerable CreateItemsSource()
        {
            string sql = "select "; 
            sql += "ProjectName," + Environment.NewLine;
            sql += "PlanName," + Environment.NewLine;
            sql += "Passed," + Environment.NewLine;
            sql += "Failed," + Environment.NewLine;
            sql += "NotExecuted," + Environment.NewLine;
            sql += "Total," + Environment.NewLine;
            sql += "PassPercent " + Environment.NewLine;
            sql += "from tblGlobalTestCasePassRate " + Environment.NewLine;
            sql += "where 1=1 "+Environment.NewLine;
            sql += strConditionFY;
            sql += strConditionProjectName;
            List<tblGlobalTestCasePassRate> items = ChartController.QuerytblGolbalTestCasePassRate(sql);

            return items;
        }

        #region basic method

        private IEnumerable itemsSource;
        private bool isDisplayed;

        protected virtual RadDataBoundListBox ListBox
        {
            get
            {
                return this.DataBoundListBox;
            }
        }

        public bool IsDisplayed
        {
            get
            {
                return this.isDisplayed;
            }
        }

        protected virtual void OnDisplayed()
        {
            if (this.ListBox == null)
            {
                return;
            }

            if (this.itemsSource == null)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(this.LoadItemsSource);
            }
            else
            {
                this.DisplayData(this.itemsSource);
            }
        }

        private void LoadItemsSource(object state)
        {
            this.itemsSource = this.CreateItemsSource();
            if (this.isDisplayed)
            {
                this.DisplayData(this.itemsSource);
            }

        }

        protected virtual bool EnableAsyncBalance
        {
            get
            {
                return true;
            }
        }

        protected virtual void DisplayData(IEnumerable items)
        {
            if (this.ListBox == null || !this.isDisplayed)
            {
                return;
            }

            this.Dispatcher.BeginInvoke(() =>
            {
                this.ListBox.BeginAsyncBalance();
                this.ListBox.ItemsSource = items;
                this.OnItemsBound();

            });
        }

        protected virtual void OnItemsBound()
        {
        }

        #endregion

    }
}